#include "Animation.h"
#include "Sprite.h"
#include <conio.h>

AnimationBase::AnimationBase()
{
	this->nFrames=0;
	this->FPSRate = 12;
	this->StartFrame=0;
	this->EndFrame=0;
	this->Pause = false;
	this->CurrentFrame=0;
	this->Loop = true;
}

AnimationBase::~AnimationBase()
{
}

void AnimationBase::AnimationForEntity(Entity *object)
{
	this->object = object;
}

void AnimationBase::SetFPSRate(int FramesPerSecond)
{
	FPSRate = FramesPerSecond;
}

void AnimationBase::SetStartFrame(int s)
{
	this->StartFrame = s;
}

void AnimationBase::SetEndFrame(int e)
{
	this->EndFrame = e;
}

void AnimationBase::SetCurrentFrame(int c)
{
	this->CurrentFrame = c;
}

void AnimationBase::Start(float dt)
{
	static float elapsedTime = 0.0f;
	elapsedTime += dt;

	if(elapsedTime >= 1.0f/FPSRate)
	{
		elapsedTime=0.0f;

		CurrentFrame++;

		if( CurrentFrame > EndFrame )
			CurrentFrame = 0;
	}

}

void AnimationBase::Stop()
{
	Pause ^= true;
}

void AnimationBase::Reset()
{
	CurrentFrame=0;
}

int AnimationBase::GetCurrentFrame()
{
	return CurrentFrame;
}
///////////////////////////////////////
///////////////////////////////////////
///////////////////////////////////////

AnimationSheet::AnimationSheet()
{
	this->textureSheet = NULL;
	nCellX = nCellY = 0;
	xInc = yInc = 0.0f;
}

AnimationSheet::~AnimationSheet()
{
	object->SetTexture(prevTexture);
	textureSheet = NULL;
	object = NULL;
}

void AnimationSheet::SetTextureSheet(Texture * texture, int cellSize)
{
	this->prevTexture = object->GetTexture();
	this->textureSheet = texture;
	object->SetTexture(texture);
	this->EndFrame = nFrames = ((texture->GetWidth()/cellSize) * (texture->GetHeight()/cellSize));
	EndFrame+=-1;

	xInc = 1.0f/((float)texture->GetWidth()/(float)cellSize);
	yInc = 1.0f/((float)texture->GetHeight()/(float)cellSize);

	nCellX = texture->GetWidth()/cellSize;
	nCellY = texture->GetHeight()/cellSize;

	texcoord[0].u = 0.0f; texcoord[0].v = 0.0f;
	texcoord[1].u = 0.0f; texcoord[1].v = yInc;
	texcoord[2].u = 0.1f; texcoord[2].v = yInc;
	texcoord[3].u = 0.1f; texcoord[3].v = 0.0f;

	((Sprite*)object)->ModifyTexCoords(texcoord);
}

void AnimationSheet::Start(float dt)
{
	static float elapsedTime=0.0f;

	elapsedTime+=dt;

	if( !Pause )
		if( elapsedTime >= 1.0f/FPSRate )
		{
			elapsedTime=0.0f;

			this->CurrentFrame++;

			if( CurrentFrame > EndFrame )
			{
				texcoord[0].u = 0.0f; texcoord[0].v = 0.0f;
				texcoord[1].u = 0.0f; texcoord[1].v = yInc;
				texcoord[2].u = xInc; texcoord[2].v = yInc;
				texcoord[3].u = xInc; texcoord[3].v = 0.0f;

				if( Loop )
					CurrentFrame=0;
				else
				{
					CurrentFrame=EndFrame;
					Pause = true;
				}
			}
			else
			{
				texcoord[0].u += xInc; texcoord[0].v = 0.0f;
				texcoord[1].u += xInc; texcoord[1].v = yInc;
				texcoord[2].u += xInc; texcoord[2].v = yInc;
				texcoord[3].u += xInc; texcoord[3].v = 0.0f;
			}

			((Sprite*)object)->ModifyTexCoords(texcoord);
		}

		_cprintf("Current Frame: %i, %i\n", CurrentFrame, nFrames);
}
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////

Animation::Animation()
{
	anim.clear();
}

Animation::~Animation()
{
	if( anim.size() > 0 )
		anim.clear();
}

void Animation::AddTextureFrame(Texture *texture)
{
	if(texture != NULL)
	{
		nFrames++;
		this->EndFrame = nFrames;

		this->anim.push_back(texture);
	}
}

void Animation::Start(float dt)
{
	if( dt >= 1.0f/FPSRate )
	{
		this->CurrentFrame++;
	}
}